<?php 
$link = mysql_connect('localhost', 'root', '');
//$link = mysql_connect('aecabill.accountsupportmysql.com', 'aeca', 'fArC5YyyG8NT9wdC5LXP');
if (!$link) { 
     die('There appeared to be an error while connecting to the website database: ' . mysql_error()); 
}
mysql_select_db(aeca);

//$agcyID = $_GET['Id'];
$agcyID = $_POST['aid'];
$query = "select * from tbl_whis_carriers";
$result = mysql_query($query);

$cquery = "select * from  tbl_companies";
$cresult = mysql_query($cquery);

$ixcquery = "select tbl_companies.c_id,tbl_companies.c_nickname,tbl_company_type.* from tbl_companies inner join tbl_company_type on tbl_companies.ct_id = tbl_company_type.ct_id where tbl_company_type.ct_company = 'IXC'";
$ixcresult = mysql_query($ixcquery);


if($agcyID)
{
$agcy_query = "select * from tbl_agency_agreement where agency_agreement_id = $agcyID";
$agy_result = mysql_fetch_array(mysql_query($agcy_query));
}

if($_POST['agency_agre'] == 'agency_agre')
{
	extract($_POST);
	if($agcyID){
		$update_query = "UPDATE tbl_agency_agreement SET qry_lec_companies='$qry_lec_companies',whls_carriers='$whls_carriers'," .
		"qry_ixc_companies='$qry_ixc_companies',effective_start_date='$effective_start_date',effective_end_date='$effective_end_date' WHERE agency_agreement_id=$agcyID";
	if(mysql_query($update_query)){?>
	<script>
	$(document).ready(function(){
	$("#success").text("Updated Successfully!").show();
	return false;
	});
	</script>
	<?}
	}else{
	$insert_query = "INSERT INTO tbl_agency_agreement (qry_lec_companies,whls_carriers,qry_ixc_companies,effective_start_date,effective_end_date) " .
			"values ('$qry_lec_companies','$whls_carriers','$qry_ixc_companies','$effective_start_date','$effective_end_date')";
	if(mysql_query($insert_query)){?>
	<script>
	$(document).ready(function(){
		$("#success").text("Inserted Successfully!").show();
		return false;
	});
	</script>
	<?}
}
if($agcyID){
$agcy_query = "select * from tbl_agency_agreement where agency_agreement_id = $agcyID";
$agy_result = mysql_fetch_array(mysql_query($agcy_query));
}
}
?>
<script>
 $(function() {  
   $(".agsubmit").click(function() { 
     // validate and process form here
     $("#agcy_frm").validate();
     var name = 'agency';
     var agid = $("#agid").val();
     var qry_lec_companies = $("#qry_lec_companies").val();
     var whls_carriers = $("#whls_carriers").val();
     var qry_ixc_companies = $("#qry_ixc_companies").val();
     var effective_start_date = $(".effective_start_date").val();
     var effective_end_date = $(".effective_end_date").val();
     if(effective_start_date != '' && effective_end_date != ''){
	     $.post("/aec1/pages/update_cprofile.php",{name:name,agid:agid,qry_lec_companies:qry_lec_companies,whls_carriers:whls_carriers,qry_ixc_companies:qry_ixc_companies,effective_start_date:effective_start_date,effective_end_date:effective_end_date},function(data){
	     	if(data == 1){
	     	$( "#dialog:ui-dialog" ).dialog( "destroy" );
				$( "#dialog-message_u" ).dialog({
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog( "close" );
						window.location = "index.php?page=company-mang";
					}
				}
				});
	     	}else if(data == 2){
	     		$( "#dialog:ui-dialog" ).dialog( "destroy" );
				$( "#dialog-message_i" ).dialog({
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog( "close" );
						window.location = "index.php?page=company-mang#tabs-2";
					}
				}
				});
	     	}
	     });
	     return false;
     }
   });  
 });
</script>
<script>
var dates = $( "#from, #to" ).datepicker({
			defaultDate: "+1w",
			changeMonth: true,
			numberOfMonths: 1,
			onSelect: function( selectedDate ) {
				var option = this.id == "from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" );
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
		});
</script>
<!-- ui-dialog -->
<div id="dialog-message_u" title="Success Update" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		The Agency Agreement has been Updated Successfully.
	</p>
</div>
<div id="dialog-message_i" title="Success Update" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
		The new Agency Agreement has been created.
	</p>
</div>
<span id='error'></span><span id='success'></span>
<fieldset>
<legend><strong><h3>Agency Agreement</h3></strong></legend>
	<div>
	<!-- <form action="index.php?page=agency-agreement<?if($agcyID){?>&Id=<?=$agcyID;}?>" method="post" id="agcy_frm"> -->
	<form action="" method="post" id="agcy_frm">
		<table>
		<tr>
			<td><label><strong>LEC Nickname</strong></label>:</td><?=$agy_result['qry_lec_companies ']?>
			<td><select name="qry_lec_companies" id="qry_lec_companies">
			<? while($crow = mysql_fetch_array($cresult)){?>
										<option value=<?=$crow['c_nickname'];?>
										<? if($agy_result['qry_lec_companies'] == $crow['c_nickname']){ echo 'selected="selected"'; } else {} ?>
										><?=$crow['c_nickname'];?></option>
			<?}?>
									</select></td>
		</tr>
		<tr>
			<td><label><strong>Wholesale Carrier CIC Code :</label></strong></td>
			<td>
			<select name="whls_carriers" id="whls_carriers">
			<? while($row = mysql_fetch_array($result)){?>
				<option value=<? echo $row['whls_carriers_id'];?> 
				<? if($agy_result['whls_carriers'] == $row['whls_carriers_id']){ echo 'selected="selected"'; } else {} ?>
				><? echo $row['whls_carriers_id'];?></option>
			<? }?>
			</select>
			</td>
		</tr>
		<tr>
			<td><label><strong>IXC Nickname :</label></strong></td>
			<td>
			<select name="qry_ixc_companies" id="qry_ixc_companies">
			<? while($ixc = mysql_fetch_array($ixcresult)){?>
				<option value=<? echo $ixc['c_nickname'];?> 
				<? if($agy_result['qry_ixc_companies'] == $ixc['c_nickname']){ echo 'selected="selected"'; } else {} ?>
				><? echo $ixc['c_nickname'];?></option>
			<? }?>
			</select>
			</td>
			</td>
		</tr>
		<tr>
			<td><label><strong>Effective Start Date* :</label></strong></td>
			<td><input type="text" name="effective_start_date" id="from" class="required effective_start_date" value="<?=$agy_result['effective_start_date']?>"/></td>
		</tr>
		<tr>
			<td><label><strong>Effective End Date* :</label></strong></td>
			<td><input type="text" name="effective_end_date" id="to" class="required effective_end_date" value="<?=$agy_result['effective_end_date']?>"/></td>
		</tr>
		<tr>
			<td>&nbsp;<input type="hidden" value="agency_agre" name="agency_agre" />
			<input type="hidden" name="adhidden" value="<?=$agcyID?>" id="agid"/></td>
			<td><input type="submit" value="submit" name="submit" class="agsubmit"/>
			<input type="button" value="Cancel" name="cancel" onclick="location.href='index.php?page=company-mang<?if(!$agcyID){?>#tabs-2<?}?>'"/>
			</td>
		</tr>
	</table>
	</form>
	</div>
</fieldset>